# Maintainer: Andrew Sun <adsun701@gmail.com>

_realname=seexpr
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.11
pkgrel=4
pkgdesc="A simple expression language (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://hoene.github.io/seexpr/"
msys2_repository_url="https://github.com/wdas/SeExpr"
license=("spdx:Apache-2.0")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-pkgconf")
source=("https://github.com/wdas/SeExpr/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "0001-seexpr-2.11-fix-cxxflags.patch"
        "0002-seexpr-2.11-fix-inline-error.patch"
        "0003-seexpr-2.11-fix-dll-installation.patch")
sha256sums=('bf4a498f86aa3fc19aad3d7384de11d5df76f7f71587c9bd789f5e50f8090e1a'
            '1eeaba08b574ca1c6ff5ce4ddcc45319dbdb3d4d9f82ca6ba9a9af32a0dbc183'
            'f7733debf6a8d0ef169abfd11eb18434a57b7e2608e49aca8131bd0f264f330f'
            'caa6576737ee72bb3961d299b7479a4a6629a90e5dde0e220ba57cef62624242')

prepare() {
  cd "${srcdir}/SeExpr-${pkgver}"
  patch -p1 -i "${srcdir}/0001-seexpr-2.11-fix-cxxflags.patch"
  patch -p1 -i "${srcdir}/0002-seexpr-2.11-fix-inline-error.patch"
  patch -p1 -i "${srcdir}/0003-seexpr-2.11-fix-dll-installation.patch"
  
  # Don't build the demo even if QT4 is found
  # sed -i 's/if(QT4_FOUND)/if(FALSE)/' "${srcdir}/SeExpr-${pkgver}/src/demos/segraph/CMakeLists.txt"

  # Don't build doc, demos and tests as they are not needed
  sed -i 's/ADD_SUBDIRECTORY (src\/doc)/ /' "${srcdir}/SeExpr-${pkgver}/CMakeLists.txt"
  sed -i 's/ADD_SUBDIRECTORY (src\/demos)/ /' "${srcdir}/SeExpr-${pkgver}/CMakeLists.txt"
  sed -i 's/ADD_SUBDIRECTORY (src\/tests)/ /' "${srcdir}/SeExpr-${pkgver}/CMakeLists.txt"
}

build() {
  # Shared Build
  mkdir -p "${srcdir}/build-${MSYSTEM}-shared" && cd "${srcdir}/build-${MSYSTEM}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
  -G"MSYS Makefiles" \
  -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
  -DCMAKE_POLICY_VERSION_MINIMUM=3.5 \
  -DCMAKE_BUILD_TYPE=Release \
  -DBUILD_SHARED_LIBS:BOOL='ON' \
  -DBUILD_TESTS:BOOL='OFF' \
  -DCMAKE_COLOR_MAKEFILE:BOOL='ON' \
  -Wno-dev \
  ../SeExpr-${pkgver}
  
  cmake --build .
  
  # Static Build
  mkdir -p "${srcdir}/build-${MSYSTEM}-static" && cd "${srcdir}/build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
  -G"MSYS Makefiles" \
  -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
  -DCMAKE_POLICY_VERSION_MINIMUM=3.5 \
  -DCMAKE_BUILD_TYPE=Release \
  -DBUILD_SHARED_LIBS:BOOL='OFF' \
  -DBUILD_TESTS:BOOL='OFF' \
  -DCMAKE_COLOR_MAKEFILE:BOOL='ON' \
  -Wno-dev \
  ../SeExpr-${pkgver}
  
  cmake --build .
}

package() {
  # Static Install
  DESTDIR="${pkgdir}" cmake --install build-${MSYSTEM}-static

  # Shared Install
  DESTDIR="${pkgdir}" cmake --install build-${MSYSTEM}-shared

  install -Dm644 "${srcdir}"/SeExpr-${pkgver}/LICENSE \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
